//
//  ViewController.m
//  MRC
//
//  Created by YaoZhaofeng on 2018/4/5.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, assign) NSString *string1;
@property (nonatomic, assign) NSString *string2;
@property (nonatomic, assign) NSString *string3;
@property (nonatomic, retain) NSArray *myArray;

@property (nonatomic, retain) NSDictionary *dicRetain;
@property (nonatomic, copy) NSDictionary *dicCopy;
@property (nonatomic, assign) NSDictionary *dicAssign;
@property (nonatomic, retain) NSMutableString *strCopy;
@end

@implementation ViewController

- (void)dealloc {
    [_myArray release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSObject *obj = [NSObject alloc];
    NSLog(@"retainCount::%lu", (unsigned long)obj.retainCount);
    
    _strCopy = [[NSMutableString alloc] initWithFormat:@"123"];
    self.dicCopy = [NSDictionary dictionaryWithObjectsAndKeys:@"key",@"value", nil];
}

//函数返回值需要添加autorelease
- (NSArray *)createArray {
    NSArray *array = [[NSArray alloc] initWithObjects:@"1", nil];
    NSLog(@"array::%@", array);
    
    return [array autorelease];
}

//不同创建方式字符串创建
- (void)memoryTest{
    NSString *str1 = @"1234567890";    TLog(str1);
    //str1: __NSCFConstantString -> 0x715ec : 1234567890  -1
    self.string1 = str1;

    NSString *str2 = [NSString stringWithString:@"1234567890"];        TLog(str2);
    //str2: __NSCFConstantString -> 0x715ec : 1234567890  -1
    self.string2 = str2;
    
    NSString *str3 = [NSString stringWithFormat:@"1234567890"];        TLog(str3);
    //str3: __NSCFString -> 0x1557cb50 : 1234567890  1

    NSString *str4 = [[[NSString alloc] initWithFormat:@"1234567890"] autorelease];  //加上autorelease相当于str3
    self.string3 = str4;
    assert(str1==str2); //一直正确
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"myArray::%lu", (unsigned long)self.myArray.retainCount);
    [self setStrCopy:_strCopy];
    NSLog(@"_strCopy::%@", _strCopy);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setDicRetain:(NSDictionary *)dicRetain {
    [dicRetain retain];
    [_dicRetain release];
    _dicRetain = dicRetain;
}

- (void)setDicCopy:(NSDictionary *)dicCopy {
    if (![_dicCopy isEqual:dicCopy]) {
        [_dicCopy release];
        
        _dicCopy = [dicCopy copy];
    }
}

- (void)setStrCopy:(NSString *)strCopy {
    NSLog(@"before::%lu", (unsigned long)_strCopy.retainCount);
    [_strCopy release];
     NSLog(@"middle::%lu", (unsigned long)_strCopy.retainCount);
    _strCopy = [strCopy retain];
     NSLog(@"after::%lu", (unsigned long)_strCopy.retainCount);
}

@end
